<?php
namespace Admin\Controller;
use Think\Controller;
class ContentsController extends Controller {
	  
    /**
     * 文章列表
     */
    public function index(){
        $model = D('article');
        $find['keyword'] = I('keyword');
        if($find['keyword']){
            $map['title'] = array('like',"%{$find['keyword']}%");
        }
        $p = I('p',1);
        $limit = I('limit',15);
        $start = ($p -1 ) * $limit;
        $count = $model->where($map)->count();
        $pag = new \Think\Page($count,$limit);

        $info['list'] = $model->where($map)->limit($start.','.$limit)->select();
        $info['page'] = $pag->show();


        $this->assign('info',$info);
        $this->display();
    }
    /**
     * 新增-编辑
     */
    public function add_data(){
        $model = D('article');
        $id = I('id');
        $title = '新增';
        $info = array();

        if($id){
            $title = '编辑';
            $info = $model->where('article_id='.$id)->find();
        }
         $class = D('articleClassify')->getClassify();

        $this->assign('class',$class);
        $this->assign('title',$title);
        $this->assign('info',$info);
        $this->display();
    }

    /**
     * 保存数据
     * @return [type] [description]
     */
    public function save_data(){
        $model = D('article');
        if(IS_POST){
            $data = $model->create();
            if($data['article_id']){
                $map['article_id'] =  $data['article_id'];
                $affect = $model->where($map)->save();
            }else{
                $affect = $model->add();
            }
            if($affect){
                $this->success('操作成功！',__CONTROLLER__.'/index');die;
            }else{
                $this->error('操作失败！');die;
            }
        }

    }

    /**
     * 文章列表
     */
    public function classify(){
        $model = D('articleClassify');
        $find['keyword'] = I('keyword');
        if($find['keyword']){
            $map['name'] = array('like',"%{$find['keyword']}%");
        }
        $p = I('p',1);
        $limit = I('limit',15);
        $start = ($p -1 ) * $limit;
        $count = $model->where($map)->count();
        $pag = new \Think\Page($count,$limit);

        $info['list'] = $model->where($map)->limit($start.','.$limit)->select();
        $info['page'] = $pag->show();

         
        $this->assign('info',$info);
        $this->display();
    } 

    /**
     * 新增-编辑
     */
    public function add_classify(){
        $model = D('articleClassify');
        $id = I('id');
        $title = '新增';
        $info = array();
        $param = '';

        if($id){
            $title = '编辑';
            $info = $model->where('classify_id',$id)->find();
            $param['classify_id'] = array('not ni'=>$id);
        } 
        $class = $model->getClassify($param);
        #dump($class);
        $this->assign('class',$class);        
        $this->assign('title',$title);
        $this->assign('info',$info);
        $this->display();
    }

    /**
     * 保存数据
     * @return [type] [description]
     */
    public function save_classify(){
        $model = D('articleClassify');
        if(IS_POST){
            $data = $model->create();
            //等级处理
            if($data['pid'] > 0){
                $level = $model->where('classify_id='.$data['pid'])->getField('level'); 
                $data['level'] = intval($level)+1;
            }
            if($data['classify_id']){
                $map['classify_id'] =  $data['classify_id'];
                unset($data['classify_id']);
                $affect = $model->where($map)->save($data);
            }else{
                $affect = $model->add($data);
            }
            if($affect){
                $this->success('操作成功！',__CONTROLLER__.'/classify');die;
            }else{
                $this->error('操作失败！');die;
            }
        }

    }
 
}